const util = require('../../utils/util.js');
const api = require('../../utils/api.js');
//获取应用实例 
var app = getApp()
Page({
	data: {
		kc: {},
		hasBaby:false,
		selectBaby:false,
		bobyList:{},
		selectBabyId:"",
		phone:"",
		code:"",
		smsTrue:false,
		countdown:60,
		smsName:"获取验证码",
	},
	onLoad: function(options) {
		let _this = this;
		wx.getStorage({
			key: 'kcJson',
			success: function(res) {
				let kc = JSON.parse(res.data);
				console.log(kc)
				_this.setData({
					kc: kc
				});
			}
		})
		this.getBobyList(app.globalData.userId);
	},
	addBaby(){
		wx.navigateTo({
			url: "../baby/baby" 
		})
	},
	selectBaby(e){
		if(this.data.selectBabyId != ""){
			if(this.data.selectBabyId == e.currentTarget.dataset.id){
				this.setData({
					selectBaby:false,
					selectBabyId:""
				});	
			}else{
				wx.showModal({
					 title: '提示',
					 content: "一个订单只能选择一个孩子",
					 showCancel:false,
					 success: function(res) {
					  
					 }				
				});
			}
		}else{
			this.setData({
				selectBaby:true,
				selectBabyId:e.currentTarget.dataset.id
			});	
		}
		
	},
	//查询宝宝
	getBobyList:function(ownerId){
		let _this = this;
		api.getBobyList({
			data: {
				ownerId: ownerId,
				pageSize: "100",
				pageNum: "1",
			},
			success: function(res) {
				if(res.data.code == 0) {
					if(res.data.list.rows != null && res.data.list.rows.length > 0){
						_this.setData({
							bobyList: res.data.list.rows,
							hasBaby:true,
						});
					}
					
				}
			},
			fail: function(res) {
				wx.showToast({
					title: res.data.msg,
					icon: 'success',
					duration: 2000
				})
			}
		});
	},
	inputPhone:function(e){
		this.setData({
	      phone: e.detail.value
	    })
	},
	inputCode:function(e){
		this.setData({
	      code: e.detail.value
	    })
	},
	setTime:function(e){
		let _this = this;
		
		if (_this.data.countdown == 0) { 
	        _this.setData({
		       smsTrue: false,
		       countdown:60,
		       smsName:"获取验证码"
		    });
	   } else { 
			let time = _this.data.countdown;
			time--;
    		_this.setData({
		       smsTrue: true,
		       smsName:time,
		       countdown:time
		    });
    	} 
		
	},
	sendSms:function(e){
		if(this.data.phone == ""){
			wx.showModal({
				 title: '提示',
				 content: "请输入手机号",
				 showCancel:false,
				 success: function(res) {
				  
				 }
			})
			return false;
		}else{
			if(!(/^1[34578]\d{9}$/.test(this.data.phone))){ 
		        wx.showModal({
					 title: '提示',
					 content: "手机号格式有误",
					 showCancel:false,
					 success: function(res) {
					  
					 }
				})  
		        return false; 
		    } 
		}
		
		let _this = this;
		api.sendSms({
			data: {
				phone:this.data.phone,
			},
			success: function(res) {
				if(res.data.code != 0) {
					wx.showModal({
						 title: '提示',
						 content: "发送验证码失败",
						 showCancel:false,
						 success: function(res) {
						  
						 }
					})
				}
			},
		});
	    let out = setInterval(function() { 
	    	if(_this.data.countdown == 0){
		    	clearInterval(out);
		    }
		    _this.setTime();
		},1000); 
	},
	subOrder:function(e) {
		if(this.data.selectBabyId == ""){
			wx.showModal({
				 title: '提示',
				 content: "请选择一个宝宝",
				 showCancel:false,
				 success: function(res) {
				  
				 }
			})
			return false;
		}
		if(e.detail.value.parentName == ""){
			wx.showModal({
				 title: '提示',
				 content: "请输入家长姓名",
				 showCancel:false,
				 success: function(res) {
				  
				 }
			})
			return false;
		}
		if(e.detail.value.parentPhone == ""){
			wx.showModal({
				 title: '提示',
				 content: "请输入家长手机号",
				 showCancel:false,
				 success: function(res) {
				  
				 }
			})
			return false;
		}
		if(e.detail.value.phoneCode == ""){
			wx.showModal({
				 title: '提示',
				 content: "请输入手机号验证码",
				 showCancel:false,
				 success: function(res) {
				  
				 }
			})
			return false;
		}
		api.orderSave({
			data: {
				babyId: this.data.selectBabyId,
				courseId: this.data.kc.courseTime.id,
				parentsId:app.globalData.userId,
				coursePoints: this.data.kc.course.coursePoints,
				parentName:e.detail.value.parentName,
				parentPhone:e.detail.value.parentPhone,
				phoneCode:e.detail.value.phoneCode,
			},
			success: function(res) {
				if(res.data.code == 0) {
					wx.navigateTo({
						url: '../order/toOrder?id='+res.data.id
					})
				}else{
					wx.showModal({
						 title: '提示',
						 content: res.data.msg,
						 showCancel:false,
						 success: function(res) {
						  
						 }
					})
				}
			},
		});

	}

})